/** 状态配置 */
import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

const store = new Vuex.Store({
    state: {
        user: {
            id: null
        }
    },
    mutations: {
        SET_LOGIN_DATA: (state, user) => {
            //更新state状态数据
            state.user = user;
            //存储到sessionStorage,防止刷新页面丢失数据
            sessionStorage.setItem("LOGIN_USER", JSON.stringify(user));
        },
        SET_LOGOUT_DATA: (state) => {
            //清除登录信息
            state.user = {
                id: null
            }
            //清除sessionStorage信息
            sessionStorage.removeItem("token");
            sessionStorage.removeItem("LOGIN_USER");
        }
    },
    actions: {
        LoginSuccess({ commit }, user) {
            commit("SET_LOGIN_DATA", user);
        },
        Logout({ commit }) {
            commit("SET_LOGOUT_DATA");
        }
    },
    getters: {
        user: state => state.user
    }
});

export default store;